<% 
	report = @report_response.report
	if not @report_response.owner_role.is_xb then 
		groups_to_iterate = report.groupings.reject {|x| x.strategy.nil?}
	else
		groups_to_iterate = report.groupings
	end
%>

<%content_for :head do%>
<script type="text/javascript">

var currentTab;

function _set_currentTab(index){	
	currentTab = index;
	if (currentTab == 0)
		jQuery('#previous').css("visibility", "hidden");
	else
		jQuery('#previous').css("visibility", "");
		
	if (currentTab == jQuery('#tabs').tabs('length')-1)
		jQuery('#next').css("visibility", "hidden");
	else
		jQuery('#next').css("visibility", "");
}

jQuery(document).ready(function(){
    jQuery("#tabs").tabs({
		select: function(event, ui) {
			_set_currentTab(ui.index);
		}
	});
	jQuery("#tabs").tabs("select", <%=@tab_selected%>);
	_set_currentTab(jQuery("#tabs").tabs('option', 'selected'));
	
	var tab = jQuery("a[href=#mainquestions7]");	
	tab.find("span").css("color", "red");
	
	jQuery(".question.text .textanswer").change(textchanged);
	jQuery(".question.yes_no .clarificationtext").change(textchanged);
	jQuery(".question.yes_no input:radio").click(yesnoclick);
	jQuery(".question.option input:radio").click(optionclick);
	jQuery(".question.option input:text").change(textchanged);
	jQuery(".question.excluding input:radio").click(excludingclick);
	
	// Show errors on tabs
	mark_error_tabs();
	
	// Auto save every 60 segs
	setInterval("auto_save()", 60000);
	
	// collapse feature
	var expand_img = '/images/b_expand.gif';
	var collapse_img = '/images/b_collapse.gif';
	var triggers = jQuery('div.trigger');
	
	triggers.prepend('<img src="' + collapse_img + '"/>&nbsp;');
	
	jQuery('img', triggers).click(function() {
		var src = jQuery(this).attr('src');
		var id = jQuery(this).parent('.trigger').attr('id');
		var collapsable = jQuery('div.collapsable[trigger=' + id + ']');
		if ( src == expand_img ) {
			jQuery(this).attr('src', collapse_img);
			collapsable.show();
		} else {
			jQuery(this).attr('src', expand_img);
			collapsable.hide();
		}
	});
});

function set_tab_selected() {
	var $tabs = jQuery('#tabs').tabs();
	var selected = $tabs.tabs('option', 'selected'); // => 0
	document.getElementById("tab_selected").value=selected;
}

function submitReport() {
	
	 set_tab_selected();
	
	var confirm_submit = <%= @confirm_submit %>;
	var answer = true;
	
	if (confirm_submit) {
		answer = confirm("At least one of the XBs hasn't submitted his response. Are you sure you want to submit your response?");
	}
	
	if (answer) {
		jQuery('#issubmitted').attr('checked', true);
		jQuery('#editform').submit();
	}
	
	return answer;
}

function submit() {
	 set_tab_selected();
	jQuery('#editform').submit();
	return true;
}

function markanswered(container, answered) {
	container.children('.deleteanswer').attr('checked', !answered);
}

function textchanged(args) {
	var parent = jQuery(this).parent();
	var text = jQuery(this).val();
	var isempty = jQuery.trim(text) == "";
	markanswered(parent, !isempty);
	auto_save();
}

function yesnoclick(args) {
	var radio = jQuery(this);
	var parent = radio.parent();
	var text = parent.children('.clarificationtext'); 
	
	text.show().val('');
	markanswered(parent, true);
	auto_save();
	
	parent.children('.clarification.yes').toggle(radio.hasClass("yes"));
	parent.children('.clarification.no').toggle(radio.hasClass("no"));
}

function optionclick(args) {
	var radio = jQuery(this);
	var parent = radio.parent();
	
	if (radio.hasClass('other')) {
		if (radio.hasClass('yes')) {
			parent.children('.other_text').show().val('');
		} else {
			parent.children('.other_text').hide();
		}
	}

	markanswered(parent, true);
	auto_save();
}

function excludingclick(args) {
	var radio = jQuery(this);
	var parent = radio.parent();
	var text = parent.children('.clarificationtext'); 
	
	text.show().val('');
	markanswered(parent, true);
	auto_save();
	
	parent.children('.clarification').show();
}

function auto_save() {
	jQuery.ajax({
		type: "POST",
		url: "/reports/auto_save/<%= @report_response.id %>",
		data: jQuery('#editform').serialize(),
		dataType: 'text',
		success: function(data) {
			jQuery('#percentage').text(data);

			jQuery('#submitreport').toggle(data == '100%');
			jQuery('#submitreport2').toggle(data == '100%');
		}
	});
}

function mark_error_tabs() {
	var size = <%= groups_to_iterate.size %>;
	for (i = 0; i < size; i++) {
		var tab_form = jQuery("div#mainquestions" + i);
		var has_error = jQuery(".formError", tab_form).size() > 0;
		if (has_error) {
			var tab = jQuery("a[href=#mainquestions" + i + "]");
			tab.find("span").css("color", "red");
		}
	}
}

function save_message() {
	var response = confirm("Press OK to save changes\nPress Cancel to discard changes");
	
	if (response == true)
	   {
		auto_save()
	   setTimeout ("window.location=\"<%= url_for :controller => 'reports', :action => 'pending' %>\";", 1000);
	   }
	   else{
	   	window.location="<%= url_for :controller => 'reports', :action => 'pending' %>";
	   }
}

function tab_previous(){
	jQuery("#tabs").tabs('select', currentTab - 1);
}

function tab_next(){
	jQuery("#tabs").tabs('select', currentTab + 1);
}
</script>

<style type="text/css">
.ui-tabs .ui-tabs-hide {
     display: none;
}
</style>
<%end%>
	
	<% form_for :report_response, @report_response, :url => { :action => :update, :id => @report_response.id }, :html => { :id => 'editform' } do |f| %> 
		<%= f.check_box :is_submitted, :class => 'hidden', :id => 'issubmitted' %>
		<%= hidden_field_tag 'tab_selected' %>
		<div id="titlereport">
			<b><%=_(@report_response.report_request.title_and_period)%> <%= _(@report_response.owner_role.description_for_report_title) %> </b>
			<span id="percentage" style ="color:#000000;"><%= @report_response.completed_description %></span> <%= _("completed") %>
			<%= link_to image_tag("b_submit_report.gif"), '#', 
				:onclick => 'submitReport()', 
				:class => ( @report_response.completed == 1 ? "b_submit" : " b_submit hidden" ), 
				:id => 'submitreport' unless @report_response.is_submitted %>
		</div>
		<div id="tabs">
			<ul>
				<li></li>
				<% groups_to_iterate.each_with_index do |group,count| %>
		            <li> <%= link_to "<span>#{group.tab_name}</span>" , "#mainquestions#{count}" %> </li>
				<% end %>
			 </ul>
			
			<div class="clear"></div>
			<div id="tabs-line"></div>
			
			<% groups_to_iterate.each_with_index do |group,count| %>       
				<div id="mainquestions<%= count%>">
					<div id="stdescription"><%= _(group.text) %></div>
					
					<% if @report_response.owner_role.is_strategy_coordinator and group.strategy != @report_response.owner_role.strategy  %>
					
					<%= render :partial => 'view_answers', 
							:collection => group.questions_for_role('sc'), 
							:as => :question, 
							:locals => { :rr_other_strategy => @repo_responses [group.strategy]  } %>
					
					<% else %>
					<%= render :partial => 'question_form', 
							:collection => group.questions.select{|q| q.available_for(@report_response.owner_role)}, 
							:as => :question, 
							:locals => { :form => f, :report_response => @report_response } %>
					<% end %>
				</div>
			<% end %>
			

		</div>
	
	<% content_for :footer do %>
		<div id="previous">
			<%= link_to_function image_tag("b_previous.gif"),  "tab_previous()"%>
		</div>
		<div id="next">
			<%= link_to_function image_tag("b_next.gif"),  "tab_next()"%>
		</div>
		<div id="save">
			<%= link_to image_tag("b_save.gif"), '#', 
				:onclick => 'submit()' unless @report_response.is_submitted%>
			<%= link_to image_tag("b_re_submit.gif"), '#', 
				:onclick => 'submitReport()', 
				:class => ( @report_response.completed == 1 ? "b_submit" : " b_submit hidden" ), 
				:id => 'submitreport3' if @report_response.is_submitted %>
		</div>
		<div id="back">
			<%= link_to_function image_tag("b_back_report_management.gif"),  "save_message()"%>
			<%= link_to image_tag("b_submit_report.gif"), '#', 
				:onclick => 'submitReport()', 
				:class => ( @report_response.completed == 1 ? "b_submit" : " b_submit hidden" ), 
				:id => 'submitreport2' unless @report_response.is_submitted %>
		</div>
	<% end %>
<% end %>